package captor.windowsystem.util;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;

import javax.swing.table.DefaultTableModel;


/**
 * @author Carlos
 *
 */
public class DomainInfoTable implements ActionListener {

    public static final long serialVersionUID = 111;
    public static final Integer TABLE_WIDTH = 432;
    public static final Integer TABLE_HEIGHT = 140;
    

    private JLabel textLabel;
    private JTable table;
    private JScrollPane scrollPanel;
    private JButton plus, minus;
    //private JButton edit;
    private DefaultTableModel tableModel;
    private JPanel mainPanel;
    
    public DomainInfoTable(String label, DefaultTableModel tableModel) {
    	this.tableModel = tableModel;
        create(label);        
    }
    
    //-------------------------------------------------------------------------   

    public void create(String label)  {
                    	    	
    	mainPanel = new JPanel();
        mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
    	
    	textLabel = new JLabel(label);
    	textLabel.setAlignmentX(JLabel.CENTER_ALIGNMENT);
        
        table = new JTable(tableModel);
        scrollPanel = new JScrollPane (table);
        
        Dimension d = new Dimension(TABLE_WIDTH,TABLE_HEIGHT);
        
        table.setSize(d);
        table.setPreferredSize(d);
        table.setMaximumSize(d);
        
        scrollPanel.setSize(d);
        scrollPanel.setPreferredSize(d);
        scrollPanel.setMaximumSize(d);
        
        //button panel
        plus = new JButton("Add");
        plus.addActionListener(this);
        minus = new JButton("Remove");
        minus.addActionListener(this);
        //edit = new JButton("Edit");
        //edit.addActionListener(this);
                
        d = new Dimension(75,25);
        
        minus.setPreferredSize(d);
        minus.setMinimumSize(d);
        minus.setMaximumSize(d);

        plus.setPreferredSize(d);
        plus.setMinimumSize(d);
        plus.setMaximumSize(d);

        //edit.setPreferredSize(d);
        //edit.setMinimumSize(d);
        //edit.setMaximumSize(d);

        JPanel bPanel = new JPanel();
        bPanel.setLayout(new BoxLayout(bPanel, BoxLayout.Y_AXIS));
        bPanel.add(Box.createRigidArea(new Dimension(5,5)));
        bPanel.add(plus);
        bPanel.add(Box.createRigidArea(new Dimension(5,5)));
        //bPanel.add(edit);
        bPanel.add(Box.createRigidArea(new Dimension(5,5)));
        bPanel.add(minus);
        bPanel.add(Box.createRigidArea(new Dimension(5,60)));
        
        JPanel tablePanel = new JPanel();
        tablePanel.setLayout(new BoxLayout(tablePanel, BoxLayout.X_AXIS));
        
        tablePanel.add(scrollPanel);
        tablePanel.add(bPanel);
        
        mainPanel.add(textLabel);
        mainPanel.add(tablePanel);
        
        table.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE); 
        
    }
        
    //-------------------------------------------------------------------------

    public void actionPerformed(ActionEvent e) {
		if ( e.getActionCommand().equals("Add") )  {			
			String[] blankLine = createLine();
			tableModel.addRow(blankLine);
		}        
		else if ( e.getActionCommand().equals("Remove") )  {
		    if ( tableModel.getRowCount() > 0 && table.getSelectedRow() >= 0 )  {
		        tableModel.removeRow(table.getSelectedRow());				
		    }
		}        
//        else if ( e.getActionCommand().equals("Edit") )  {
//        	int row = table.getSelectedRow();
//        	if ( row != -1 )  {
//    		    //TableValuesPanel tvp = new TableValuesPanel(model, this, tableModel, row);
//    		    //tvp.setVisible(true);
//        	}
//        }		
    }

    //-------------------------------------------------------------------------

    public void setLabel(String label)  {
        textLabel.setText(label);
    }
    
    //-------------------------------------------------------------------------

    public void setTableModel(DefaultTableModel tableModel)  {
        Vector data = tableModel.getDataVector();
        for ( int i = 0; i < data.size(); i++ )  {
            Vector v = (Vector) data.get(i);
            this.tableModel.addRow(v);    
        }
    }

    //-------------------------------------------------------------------------

    public boolean validateFields()  {

        for ( int i = 0; i < tableModel.getRowCount(); i++ )  {
            for ( int j = 0; j < tableModel.getColumnCount(); j++ )  {
               
   		        }
            }                
        return true;
    }
    
    //-------------------------------------------------------------------------

	public JPanel getMainPanel() {
		return mainPanel;
	}
	
    //-------------------------------------------------------------------------
	
	public void setEnabled(boolean enabled){
		
		if (mainPanel!=null)
			mainPanel.setVisible(enabled);	    
	}
    //-------------------------------------------------------------------------
	
	private String[] createLine(){
		
		String[] line = new String [tableModel.getColumnCount()];
				
		for ( int i = 0; i < tableModel.getColumnCount(); i++ )  {
			line[i]= "";
		}			
		return line;
	}
	
	public DefaultTableModel getTableModel(){
		return tableModel;
	}
	
	public Object getValueAt(int row, int column){
		return tableModel.getValueAt(row, column);
	}
	
	public int getColumnCount(){
		return tableModel.getColumnCount();
	}
	
	public int getRowCount(){
		return tableModel.getRowCount();
	}
}